package com.hunttown.mes.convert;

import com.google.common.collect.Lists;
import com.hunttown.mes.common.utils.BeanUtils;
import com.hunttown.mes.domain.TransportPurchase;
import com.hunttown.mes.rpc.domain.TransportPurchaseDTO;
import com.hunttown.mes.rpc.exception.ConvertException;
import com.hunttown.mes.rpc.domain.enums.ErrorCode;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Collections;
import java.util.List;

/**
 * created by wangjunfu on 2019-05-18 20:13:40
 */
public class TransportPurchaseConverts {

    private static Logger LOG = LoggerFactory.getLogger(TransportPurchaseConverts.class);

    public static List<TransportPurchaseDTO> convert(List<TransportPurchase> transportPurchaseList) {
        if (CollectionUtils.isEmpty(transportPurchaseList)) {
            return Collections.EMPTY_LIST;
        }

        List<TransportPurchaseDTO> transportPurchaseListResult = Lists.newArrayList();
        for (TransportPurchase transportPurchase : transportPurchaseList) {
            transportPurchaseListResult.add(convert(transportPurchase));
        }
        return transportPurchaseListResult;
    }

    public static TransportPurchaseDTO convert(TransportPurchase transportPurchase) {
        try {
            return BeanUtils.copy(transportPurchase, TransportPurchaseDTO.class);
        } catch (Exception e) {
            LOG.error("TransportPurchase到TransportPurchaseDTO转换失败!", e);
            throw new ConvertException(ErrorCode.CONVERT_ERROR, "TransportPurchase到TransportPurchaseDTO转换失败", e);
        }
    }

    public static TransportPurchase convert(TransportPurchaseDTO transportPurchaseDTO) {
        try {
            return BeanUtils.copy(transportPurchaseDTO, TransportPurchase.class);
        } catch (Exception e) {
            LOG.error("TransportPurchaseDTO到TransportPurchase转换失败!", e);
            throw new ConvertException(ErrorCode.CONVERT_ERROR, "TransportPurchaseDTO到TransportPurchase转换失败", e);
        }
    }
}